package fanglu.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

/**
 * @author Fang Lu, fanglupku@gmail.com
 */

public class FileDealer {
	public static String getFileContent(String filename) throws IOException {
		File file = new File(filename);
		FileReader fr;
		BufferedReader br;
		String oneLine;
		StringBuffer sb = new StringBuffer();

		fr = new FileReader(file);
		br = new BufferedReader(fr);
		oneLine = br.readLine();

		while (oneLine != null) {
			sb.append(oneLine);
			sb.append("\n");
			oneLine = br.readLine();
		}

		br.close();
		br = null;
		fr.close();
		fr = null;
		file = null;

		return sb.toString();
	}

	public static String getFileContent(String filename, String encode)
			throws IOException {
		File file = new File(filename);
		FileInputStream fis;
		InputStreamReader isr;
		BufferedReader br;
		String oneLine;
		StringBuffer sb = new StringBuffer();

		fis = new FileInputStream(file);
		if (encode == null) {
			isr = new InputStreamReader(fis);
		} else {
			isr = new InputStreamReader(fis, encode);
		}
		br = new BufferedReader(isr);
		oneLine = br.readLine();

		while (oneLine != null) {
			sb.append(oneLine);
			sb.append("\n");
			oneLine = br.readLine();
		}
		br.close();
		br = null;
		isr.close();
		isr = null;
		fis.close();
		fis = null;
		file = null;

		return sb.toString();
	}

	public static int saveFile(String content, String filename)
			throws IOException {
		File file = new File(filename);
		FileOutputStream fos;
		OutputStreamWriter osw;
		BufferedWriter bw;

		fos = new FileOutputStream(file);
		osw = new OutputStreamWriter(fos);
		bw = new BufferedWriter(osw);
		bw.write(content);

		bw.close();
		bw = null;
		osw.close();
		osw = null;
		fos.close();
		fos = null;
		file = null;

		return 0;
	}

	public static int saveFile(String content, String filename, String encode,
			boolean append) throws IOException {
		File file = new File(filename);
		FileOutputStream fos;
		OutputStreamWriter osw;
		BufferedWriter bw;

		fos = new FileOutputStream(file, append);
		if (encode == null) {
			osw = new OutputStreamWriter(fos);
		} else {
			osw = new OutputStreamWriter(fos, encode);
		}
		bw = new BufferedWriter(osw);
		bw.write(content);

		bw.close();
		bw = null;
		osw.close();
		osw = null;
		fos.close();
		fos = null;
		file = null;

		return 0;
	}

	// 复制文件
	public static void copyFile(File sourceFile, File targetFile)
			throws IOException {
		// 新建文件输入流并对它进行缓冲
		FileInputStream input = new FileInputStream(sourceFile);
		BufferedInputStream inBuff = new BufferedInputStream(input);

		// 新建文件输出流并对它进行缓冲
		FileOutputStream output = new FileOutputStream(targetFile);
		BufferedOutputStream outBuff = new BufferedOutputStream(output);

		// 缓冲数组
		byte[] b = new byte[1024 * 5];
		int len;
		while ((len = inBuff.read(b)) != -1) {
			outBuff.write(b, 0, len);
		}
		// 刷新此缓冲的输出流
		outBuff.flush();

		// 关闭流
		inBuff.close();
		outBuff.close();
		output.close();
		input.close();
	}

	// 复制文件夹
	public static void copyDirectiory(String sourceDir, String targetDir)
			throws IOException {
		// 新建目标目录
		(new File(targetDir)).mkdirs();
		// 获取源文件夹当前下的文件或目录
		File[] file = (new File(sourceDir)).listFiles();
		for (int i = 0; i < file.length; i++) {
			if (file[i].isFile()) {
				// 源文件
				File sourceFile = file[i];
				// 目标文件
				File targetFile = new File(new File(targetDir)
						.getAbsolutePath()
						+ File.separator + file[i].getName());
				copyFile(sourceFile, targetFile);
			}
			if (file[i].isDirectory()) {
				// 准备复制的源文件夹
				String dir1 = sourceDir + "/" + file[i].getName();
				// 准备复制的目标文件夹
				String dir2 = targetDir + "/" + file[i].getName();
				copyDirectiory(dir1, dir2);
			}
		}
	}
}
